﻿Public Class Bewerkincident
    Dim incidents As New Incidents
    Dim users As New Users
    Dim customers As New Customers
    Dim incident_id As Integer
    Dim klantovnr As String
    Dim alleusers As Collection = New Collection

    Public Function weergeefIncident(ByVal incid_id As String, Optional ByVal nieuw As Boolean = False) 'nieuw wordt nog niet gebruikt.
        If nieuw = False Then
            incidents.LoadFromDB(incid_id)
            incident_id = incidents.Id
            Apparaat_TB.Text = incidents.apparaat
            Categorie_TB.Text = incidents.categorie
            Beschrijvingprobleem_TB.Text = incidents.beschrijving
            Oplossing_TB.Text = incidents.oplossing


            If incidents.status.ToLower = "geopend" Then
                StatusGeopend_RB.Checked = True
            ElseIf incidents.status.ToLower = "inbehandeling" Then
                StatusInbehandeling_RB.Checked = True
            ElseIf incidents.status.ToLower = "gesloten" Then
                StatusGesloten_RB.Checked = True
            End If

            Ovnummer_TB.Text = incidents.ovnummer
            If customers.LoadAllFromDB() = "True" Then
                For Each row As DataRow In customers.KlantenDataSet.Tables("Table").Rows()
                    If row.Item("ovnummer").ToString() = incidents.ovnummer Then
                        customers.LoadFromDB(row.Item("id").ToString())
                        Ovnummer_TB.Text = customers.voorletter + ". " + customers.achternaam + " (" + incidents.ovnummer + ")"
                        Exit For
                    End If
                Next
            End If

            'users.LoadFromDB(incidents.behandelaar)

            If (incidents.prioriteit.ToLower() = "laag") Then
                PrioriteitLaag_RB.Checked = True
            ElseIf (incidents.prioriteit.ToLower() = "normaal") Then
                PrioriteitNormaal_RB.Checked = True
            ElseIf (incidents.prioriteit.ToLower() = "hoog") Then
                PrioriteitHoog_RB.Checked = True
            End If

            Impact_TB.Text = incidents.impact
            Deadline_TB.Text = incidents.deadline
            EersteBehandelaar_TB.Text = incidents.eerstebehandelaar
            Aangemeld_TB.Text = incidents.aangemeld + " " + incidents.tijd
            Extra_TB.Text = incidents.extra
            Topdesk_TB.Text = incidents.topdesk

            ComboBox1.Items.Clear()
            alleusers.Clear()
            alleusers.Add("0")
            users.LoadAllFromDB()
            For Each row As DataRow In users.UsersDataSet.Tables("Table").Rows()
                If row.Item("klas").ToString() = "" Then
                    ComboBox1.Items.Add(row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString())
                Else
                    ComboBox1.Items.Add(row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString() + " (" + row.Item("klas").ToString() + ")")
                End If
                 alleusers.Add(row.Item("ovnummer").ToString())
                If row.Item("ovnummer").ToString() = incidents.behandelaar Then
                    If Not row.Item("klas").ToString() = "" Then
                        Behandelaar_TB.Text = row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString() + " (" + row.Item("klas").ToString() + ")"
                    Else
                        Behandelaar_TB.Text = row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString()
                    End If
                End If
                If incidents.behandelaar = row.Item("ovnummer").ToString() Then
                    If row.Item("klas").ToString() = "" Then
                        ComboBox1.SelectedItem = row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString()
                    Else
                        ComboBox1.SelectedItem = row.Item("name").ToString() + " - " + row.Item("ovnummer").ToString() + " (" + row.Item("klas").ToString() + ")"
                    End If

                End If
            Next
        End If
        klantovnr = incidents.ovnummer
        Me.Show()
        Return True
    End Function

    Private Sub Opslaan_B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Opslaan_B.Click
        incidents.clearData()
        incidents._Id = incident_id
        incidents.ovnummer = klantovnr
        incidents.apparaat = Apparaat_TB.Text
        incidents.beschrijving = Beschrijvingprobleem_TB.Text
        incidents.oplossing = Oplossing_TB.Text
        incidents.behandelaar = alleusers.Item(ComboBox1.SelectedIndex + 2)
        incidents.apparaat = Apparaat_TB.Text
        incidents.categorie = Categorie_TB.Text
        If StatusGeopend_RB.Checked = True Then
            incidents.status = "Geopend"
        ElseIf StatusInbehandeling_RB.Checked = True Then
            incidents.status = "Inbehandeling"
        ElseIf StatusGesloten_RB.Checked = True Then
            incidents.status = "Gesloten"
        End If
        If PrioriteitHoog_RB.Checked = True Then
            incidents.prioriteit = "Hoog"
        ElseIf PrioriteitNormaal_RB.Checked = True Then
            incidents.prioriteit = "Normaal"
        ElseIf PrioriteitLaag_RB.Checked = True Then
            incidents.prioriteit = "Laag"
        End If
        incidents.impact = Impact_TB.Text
        incidents.deadline = Deadline_TB.Text
        incidents.extra = Extra_TB.Text
        incidents.topdesk = Topdesk_TB.Text
        incidents.eerstebehandelaar = EersteBehandelaar_TB.Text

        incidents.SaveToDB(incident_id, False) 'verander false straks zodra de mogelijkheid voor het maken van nieuwe incidenten bestaat.
        If incidents.LoadAllFromDB() = "True" Then
            AdminmenuV2.Weergave_CB.SelectedIndex = 0
        End If
        AdminmenuV2.Show()
        AdminmenuV2.refreshCurrentList()
        Me.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Behandelaar_TB.Text = ComboBox1.SelectedItem.ToString
    End Sub

    Private Sub PrioriteitLaag_RB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrioriteitLaag_RB.CheckedChanged
        PrioriteitBar_PB.Width = 66
    End Sub

    Private Sub PrioriteitNormaal_RB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrioriteitNormaal_RB.CheckedChanged
        PrioriteitBar_PB.Width = 133
    End Sub

    Private Sub PrioriteitHoog_RB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrioriteitHoog_RB.CheckedChanged
        PrioriteitBar_PB.Width = 200
    End Sub
End Class